﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.FrmKP
{
    public partial class FrmReturn_SK_FPHM : Form
    {
        private int _id;

        /// <summary>
        /// 修正税控发票号码
        /// </summary>
        public FrmReturn_SK_FPHM(int id, string fpdm, string fphm,DateTime ddate)
        {
            InitializeComponent();
            this._id = id;
            this.Icon = YTSK.Properties.Resources.Fly;
            SetFPHM(fpdm, fphm, ddate);
            setLabel();
        }
        private void SetFPHM(string fpdm, string fphm,DateTime ddate)
        {
            this.tbfpdm.Text = fpdm;
            this.tbfphm.Text = fphm;
            this.dateTimePicker1.Value = ddate;
        }

        #region 设置label的信息
        private void setLabel()
        {
            this.dateTimePicker1.Enabled = true;
            this.Text = "发票号码修正";
            this.lbtip.Text = "请输入修正后的发票信息：";
            this.btOK.Text = "确认修正";
        }
        #endregion


        #region 回写发票
        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/ModifyVouchCode?token={1}",
                    ServerConfig.ServerAddress, ServerConfig.GetToken());
                var pos = new Dictionary<string, object>();
                pos.Add("ID", _id);
                string fpdm = this.tbfpdm.Text.Trim();
                string fphm = this.tbfphm.Text.Trim();
                var err = CheckFP(fpdm, fphm);
                if (err.Length > 0) { MessageBox.Show(err); return; }
                pos.Add("fpdm", fpdm);
                pos.Add("fphm", fphm);
                var ddate = this.dateTimePicker1.Value;
                ddate = new DateTime(ddate.Year, ddate.Month, ddate.Day);
                pos.Add("dFPDate", ddate);

                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("回写发票号出现错误" + result.resultdetail); return; }
                else
                {
                    MessageBox.Show("发票号码修正成功!");
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 检查发票号码及发票代码的长度
        private string CheckFP(string fpdm, string fphm)
        {
            var err = "";
            //统一为8位
            if (fphm.Length != 8)
            {
                err += string.Format("发票号码的长度必须为8位，[{0}]的长度[{1}]位不正确", fphm, fphm.Length);
            }
            //电子发票12位，普通发票和专用发票10位
            if (fpdm.Length != 10 && fpdm.Length != 12)
            {
                err += string.Format("发票代码的长度必须为10或12位，[{0}]的长度[{1}]位不正确", fpdm, fpdm.Length);
            }
            return err;
        }
        #endregion


        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
